package com.doudou.base.resources.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doudou.base.resources.dao.UserRealInfoDao;
import com.doudou.base.resources.entity.UserRealInfo;
import com.doudou.base.resources.entity.dto.user.realinfo.UserRealInfoAddDto;
import com.doudou.base.resources.entity.dto.user.realinfo.UserRealInfoUpdateDto;
import com.doudou.base.resources.service.UserRealInfoService;
import com.doudou.client.entity.UserRealInfoDto;
import com.doudou.commons.utils.ConvertBeanUtils;
import com.doudou.commons.utils.ListUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**>
 * 用户的真实信息 服务实现类
 *
 * @author 傻男人
 * @since 2020-05-14
 */
@Slf4j
@Service
public class UserRealInfoServiceImpl  implements UserRealInfoService {

    @Autowired
    UserRealInfoDao userRealInfoDao;

    /**
    * 添加用户的真实信息
    * @param userRealInfoAddDto
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserRealInfoDto addUserRealInfo(UserRealInfoAddDto userRealInfoAddDto) {
        UserRealInfo userRealInfo = ConvertBeanUtils.copyProperties(userRealInfoAddDto,UserRealInfo.class);
        userRealInfoDao.insert(userRealInfo);
        return ConvertBeanUtils.copyProperties(userRealInfo,UserRealInfoDto.class);
    }

    /**
     * 根据ids删除用户的真实信息
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUserRealInfo(String ids) {
        List<Long> listId = ListUtils.strToLongList(ids);
        QueryWrapper<UserRealInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(UserRealInfo.Meta.id,listId);

        UserRealInfo userRealInfo = new UserRealInfo();
        userRealInfo .setDelFlag(1);
        userRealInfo .setUpdateDate(new Date());
        return userRealInfoDao.update(userRealInfo, queryWrapper);
    }

    /**
     * 根据id修改用户的真实信息
     * @param userRealInfoUpdateDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateUserRealInfo(UserRealInfoUpdateDto userRealInfoUpdateDto) {
        //检查传入的id是否正确
        UserRealInfo userRealInfo = userRealInfoDao.selectById(userRealInfoUpdateDto.getId());
        if(null == userRealInfo){
            log.info("修改的数据不存在");
            return null;
        }
        userRealInfo = ConvertBeanUtils.copyProperties(userRealInfoUpdateDto,UserRealInfo.class);
        return userRealInfoDao.updateById(userRealInfo);
    }

    /**
     * 根据ids查询用户的真实信息
     * @param ids
     * @return
     */
    @Override
    public List<UserRealInfoDto> findUserRealInfo(String ids) {
        List<Long> listIds = ListUtils.strToLongList(ids);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in(UserRealInfo.Meta.id,listIds);
        queryWrapper.eq(UserRealInfo.Meta.delFlag,0);
        List<UserRealInfo> userRealInfos = userRealInfoDao.selectList(queryWrapper);
        return ListUtils.copyList(userRealInfos,UserRealInfoDto.class);
    }

    /**
     * 根据id查询用户的真实信息
     * @param primaryId
     * @return
     */
    @Override
    public UserRealInfoDto findUserRealInfoById(Long primaryId){
        UserRealInfo userRealInfo = userRealInfoDao.selectById(primaryId);
        if(userRealInfo == null || userRealInfo.getDelFlag() != 0 ){
            return null;
        }
        return ConvertBeanUtils.copyProperties(userRealInfo,UserRealInfoDto.class);
    }


}
